package com.example.apartment.controller;

import com.example.apartment.common.ApiRestResponse;
import com.example.apartment.common.RespExceptionEnum;
import com.example.apartment.domain.Admin;
import com.example.apartment.domain.Student;
import com.example.apartment.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.List;

/**
 * 学生用户控制类
 *
 * @author wuzexin
 * @date 2021/10/28 14:34
 */
@RestController
@RequestMapping("/stu")
public class StudentController {

    @Resource
    private StudentService studentService;

    @PostMapping("/test")
    public ApiRestResponse test(String name, int age) {
        return ApiRestResponse.success("hello, " + name + "," + age);
    }

    @PostMapping("/login")
    public ApiRestResponse login(String sid, String password, HttpSession session) {
        // 判断学号是否为空
        if (sid.isEmpty()) {
            return ApiRestResponse.error(RespExceptionEnum.NEED_USER_NAME);
        }
        // 判断密码是否为空
        if (password.isEmpty()) {
            return ApiRestResponse.error(RespExceptionEnum.NEED_PASSWORD);
        }
        // 进行登录
        Student student = studentService.login(sid, password);
        // 登录成功， 将用户信息存入session
        session.setAttribute("student", student);

        System.out.println(session.getAttribute("student"));
        System.out.println(session.getAttribute("admin"));

        return ApiRestResponse.success(student);
    }

    /*
        退出登录
     */
    @GetMapping("/logout")
    public ApiRestResponse logout(HttpSession session) {
        if (session.getAttribute("student") != null) {
            session.removeAttribute("student");
        }
        return ApiRestResponse.success();
    }

    /*
        查询学生信息
     */
    @PostMapping("/select")
    public ApiRestResponse select(HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 查询学生信息
        List<Student> list = studentService.select(admin);

        // 返回学生信息
        return ApiRestResponse.success(list);
    }

    /*
        新增学生
     */
    @PostMapping("addStu")
    public ApiRestResponse addStu(String sid, String name, Integer sex, String nation, String major, String classroom, String cellphone, String password, String buildingName, Integer roomNum, Integer bedNum, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        Student student = studentService.addStudent(admin, sid, name, sex, nation, major, classroom, cellphone, password, buildingName, roomNum, bedNum);

        return ApiRestResponse.success(student);
    }

    /*
        删除学生
     */
    @PostMapping("delStu")
    public ApiRestResponse delStu(Integer id, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 删除学生
        studentService.delStudent(admin, id);

        return ApiRestResponse.success();
    }

    /*
        根据id查询学生信息
     */
    @PostMapping("/selectById")
    public ApiRestResponse selectById(Integer id, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 查询学生信息
        Student stu = studentService.selectById(admin, id);

        // 返回学生信息
        return ApiRestResponse.success(stu);
    }

    /*
        修改学生信息
     */
    @PostMapping("/editStu")
    public ApiRestResponse editStudent(Integer id, String sid, String name, Integer sex, String nation, String major, String classroom, String cellphone, String password, String buildingName, Integer roomNum, Integer bedNum, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        Student student = studentService.editStudent(admin, id, sid, name, sex, nation, major, classroom, cellphone, password, buildingName, roomNum, bedNum);

        return ApiRestResponse.success(student);
    }

    /*
        查询所有未分配床位学生
     */
    @PostMapping("/selectNoBed")
    public ApiRestResponse selectNoBed(HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        List<Student> list = studentService.selectNoBed(admin);

        return ApiRestResponse.success(list);
    }

    /*
        学生查询个人信息
     */
    @PostMapping("/selectSelf")
    public ApiRestResponse selectSelf(HttpSession session) {

        // 从session获取学生用户信息
        Student student = (Student) session.getAttribute("student");

        // 如果student为空，则抛出未登录异常
        if (student == null) {
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 查询学生个人信息
        Student stu = studentService.selectSelf(student);

        return ApiRestResponse.success(stu);

    }

    /*
        查询室友
     */
    @PostMapping("/selectRoommate")
    public ApiRestResponse selectRoommate(HttpSession session) {

        // 从session获取学生用户信息
        Student student = (Student) session.getAttribute("student");

        // 如果student为空，则抛出未登录异常
        if (student == null) {
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 查询室友信息
        List<Student> list = studentService.selectRoommate(student);

        return ApiRestResponse.success(list);

    }

    /*
        查询所有班级列表
     */
    @PostMapping("/selectAllClassroom")
    public ApiRestResponse selectAllClassroom(HttpSession session) {

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null) {
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        List list = studentService.selectAllClassroom(admin);

        return ApiRestResponse.success(list);
    }

    /*
        查询某个班级未分配床位数量学生
     */
    @PostMapping("/selectNoBedStuCount")
    public ApiRestResponse selectNoBedStuCount(String classroom, String sex, HttpSession session) {

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null) {
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        int gender = Integer.valueOf(sex);

        int count = studentService.selectNoBedStuCount(admin, classroom, gender);

        return ApiRestResponse.success(count);
    }

    /*
        根据班级和宿舍楼自动分配宿舍
     */
    @PostMapping("/autoSetApartment")
    public ApiRestResponse autoSetApartment(String classroom, String buildingName, String sex, HttpSession session) {

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null) {
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        int gender = Integer.valueOf(sex);

        int count = studentService.autoSetApartment(admin, classroom, buildingName, gender);

        return ApiRestResponse.success(count);
    }
}
